package com.navi.loaddata.loadService.impl.EqEvent;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.EtlEquipEventV;
import com.navi.loaddata.core.oracleEntity.EtlEquipEventVMapper;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveEqAlarm {
    private static final Logger logger = LoggerFactory.getLogger(SaveEqAlarm.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());


    @Autowired
    private OracleRepositoryImpl dao3;


    public void saveEqAlarmList1000(EtlConfDModel etlConfD, Map<String, EtlEquipEventV> saveList) {
        Session session = dao3.getEntityManagerSession();
        String SQLAProcedure = "{Call ARRAY_MES_LOADER.LOAD_EQUIP_EVENT()}";
        String SQLFProcedure = "{Call CF_MES_LOADER.LOAD_EQUIP_EVENT()}";
        String SQLCProcedure = "{Call CELL1_MES_LOADER.LOAD_EQUIP_EVENT()}";
        String SQLACProcedure = "{Call AC_MES_LOADER.LOAD_EQUIP_EVENT()}";
        for(EtlEquipEventV eq:saveList.values()) {
            try {
                switch (eq.getShop()) {
                    case "ARRAY":
                        dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToArray(eq));
                        break;
                    case "CF":
                        dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToCf(eq));
                        break;
                    case "ACF":
                        dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToAcf(eq));
                        break;
                    case "OC":
                        if ("CELL1".equals(etlConfD.getCellType())) {
                            dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToCell1(eq));
                        }
                        break;
                }
            } catch (Exception e) {
                throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_EQUIPEVENT, e);
            }
        }


//         call proc
        try {
            SQLQuery queryA = session.createSQLQuery(SQLAProcedure);
            SQLQuery queryF = session.createSQLQuery(SQLFProcedure);
            SQLQuery queryC = session.createSQLQuery(SQLCProcedure);
            SQLQuery queryAC = session.createSQLQuery(SQLACProcedure);
            queryA.executeUpdate();
            queryF.executeUpdate();
            queryC.executeUpdate();
            queryAC.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_EQUIPEVENT, e);
        }

    }

    public void deleteEqAlarmList1000(EtlConfDModel etlConfD) {

        Session session = dao3.getEntityManagerSession();
        String hqlEqAlarmA = "Delete FROM EtlArrayEquipEventV";
        String hqlEqAlarmF = "Delete FROM EtlCfEquipEventV";
        String hqlEqAlarmC = "Delete FROM EtlCellEquipEventV";
        String hqlEqAlarmAC = "Delete FROM EtlAcCmnEquipEventV";

        try {
            Query qEqAlarmA = session.createQuery(hqlEqAlarmA);
            Query qEqAlarmF = session.createQuery(hqlEqAlarmF);
            Query qEqAlarmC = session.createQuery(hqlEqAlarmC);
            Query qEqAlarmAC = session.createQuery(hqlEqAlarmAC);
            qEqAlarmA.executeUpdate();
            qEqAlarmF.executeUpdate();
            qEqAlarmC.executeUpdate();
            qEqAlarmAC.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_EQUIPEVENT, e);
        }
    }

}
